﻿<template>
  <a-card :bordered="false">
    <div class="table-operator">
      <a-button
        v-if="hasPerm('Base_DictionaryMain.Add')"
        type="primary"
        icon="plus"
        @click="handleAdd()"
      >新建</a-button>
      <a-button
        v-if="hasPerm('Base_DictionaryMain.Delete')"
        type="primary"
        icon="minus"
        @click="handleDelete(selectedRowKeys)"
        :disabled="!hasSelected()"
        :loading="loading"
      >删除</a-button>
      <a-button type="primary" icon="redo" @click="getDataList()">刷新</a-button>
    </div>
    <div class="table-page-search-wrapper">
      <a-form layout="inline">
        <a-row :gutter="10">
          <a-col :md="4" :sm="24">
            <a-form-item label="查询类别">
              <a-select v-model="queryParam.condition">
                <a-select-option key="DicName">字典名称</a-select-option>
                <a-select-option key="DicKey">字典Key</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md="4" :sm="24">
            <a-form-item>
              <a-input allowClear @keydown="inputKeyDown" v-model="queryParam.keyword" placeholder="关键字" />
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="24">
            <a-button type="primary" @click="() => {this.pagination.current = 1; this.getDataList()}">查询</a-button>
            <a-button style="margin-left: 8px" @click="() => ( this.queryParam = { condition: 'DicName',keywords: '' })">重置</a-button>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <a-table
      ref="table"
      :scroll="{ y: tableHight }"
      :columns="columns"
      :rowKey="(row, index) => row.Id"
      :dataSource="data"
      :pagination="pagination"
      :loading="loading"
      @change="handleTableChange"
      :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange, type: 'checkbox' }"
      :bordered="true"
      size="small">
      <span slot="CreateTime" slot-scope="CreateTime">{{ timeFormat(CreateTime,'YYYY-MM-DD HH:mm') }}</span>
      <span slot="ModifyDate" slot-scope="ModifyDate">{{ timeFormat(ModifyDate,'YYYY-MM-DD HH:mm') }}</span>
      <span slot="DictionaryDetailsCount" slot-scope="DictionaryDetailsCount">
        <a-tag color="blue" style="width: 100%">{{ DictionaryDetailsCount }}</a-tag>
      </span>
      <span slot="Enable" slot-scope="Enable">
        <a-tag color="red" v-if="Enable == 0">否</a-tag>
        <a-tag color="green" v-else-if="Enable == 1">是</a-tag>
        <a-tag color="blue" v-else>否</a-tag>
      </span>
      <span slot="IsSystem" slot-scope="IsSystem">
        <a-tag color="red" v-if="IsSystem == 0">否</a-tag>
        <a-tag color="green" v-else-if="IsSystem == 1">是</a-tag>
        <a-tag color="blue" v-else>否</a-tag>
      </span>
      <span slot="action" slot-scope="text, record">
        <template>
          <a v-if="hasPerm('Base_DictionaryMain.Edit')" @click="handleEdit(record.Id)">编辑</a>
          <a-divider
            v-if="hasPerm('Base_DictionaryMain.Edit') && hasPerm('Base_DictionaryMain.Delete')"
            type="vertical"
          />
          <a v-if="hasPerm('Base_DictionaryMain.Delete')" @click="handleDelete([record.Id])">删除</a>
        </template>
      </span>
    </a-table>
    <edit-form ref="editForm" :parentObj="this"></edit-form>
  </a-card>
</template>

<script>
import EditForm from './EditForm'

const columns = [
  { title: '字典名称', dataIndex: 'DicName', width: '10%', align: 'left' },
  { title: '字典Key值', dataIndex: 'DicKey', width: '10%', align: 'left' },
  { title: '子项数量', dataIndex: 'DictionaryDetailsCount', width: '5%', align: 'center', scopedSlots: { customRender: 'DictionaryDetailsCount' } },
  { title: '是否启用', dataIndex: 'Enable', width: '5%', align: 'center', scopedSlots: { customRender: 'Enable' } },
  { title: '系统必备', dataIndex: 'IsSystem', width: '5%', align: 'center', scopedSlots: { customRender: 'IsSystem' } },
  {
    title: '创建时间',
    dataIndex: 'CreateTime',
    align: 'center',
    width: '10%',
    scopedSlots: { customRender: 'CreateTime' }
  },
  {
    title: '最后修改时间',
    dataIndex: 'ModifyDate',
    align: 'center',
    width: '10%',
    scopedSlots: { customRender: 'ModifyDate' }
  },
  // { title: '父级ID', dataIndex: 'ParentId' },
  // { title: '配置项', dataIndex: 'Config' },
  // { title: 'SQL语句', dataIndex: 'DbSql' },
  // { title: 'DBServer', dataIndex: 'DBServer' },
  // { title: '修改人ID', dataIndex: 'ModifyID' },
  // { title: '修改人姓名', dataIndex: 'Modifier', width: '5%', align: 'center' },
  { title: '备注', dataIndex: 'Remark' },
  { title: '操作', dataIndex: 'action', align: 'left', scopedSlots: { customRender: 'action' } }
]

export default {
  components: {
    EditForm
  },
  mounted () {
    this.$nextTick(() => {
      // 页面渲染完成后的回调
      this.tableHight = this.getTableHeight(true) + 24.5
    })
    this.getDataList()
  },
  data () {
    return {
      tableHight: 570, // 表格高度
      disabled: false,
      data: [],
      pagination: {
        current: 1,
        pageSize: 25,
        showTotal: (total, range) => `总数:${total} 当前:${range[0]}-${range[1]}`,
        pageSizeOptions: ['25', '50', '100'], // 设置pageSize的可选值，页面可以通过下拉框进行选择
        showSizeChanger: true
      },
      filters: {},
      sorter: { field: 'Id', order: 'desc' },
      loading: false,
      columns,
      queryParam: { condition: 'DicName', keywords: '' },
      selectedRows: [],
      selectedRowKeys: []
    }
  },
  methods: {
    handleTableChange (pagination, filters, sorter) {
      this.filters = { ...filters }
      this.getDataList()
    },
    getDataList () {
      this.selectedRowKeys = []
      this.loading = true
      this.$http.post('/Base_BasicData/Base_DictionaryMain/GetDataListBySingleCondition', {
        PageIndex: this.pagination.current,
        PageRows: this.pagination.pageSize,
        SortField: this.sorter.field || 'Id',
        SortType: this.sorter.order,
        Search: this.queryParam,
        ...this.filters
      }).then(resJson => {
        this.loading = false
        this.data = resJson.Data
        if (this.data) {
          const pagination = { ...this.pagination }
          pagination.total = resJson.Total
          this.pagination = pagination
        }
      })
    },
    inputKeyDown (e) {
      if (e.keyCode === 13) {
        if (!this.queryParam[0].keyword) return this.$message.error('请输入查询条件')
        this.getDataList()
      }
    },
    onSelectChange (selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },
    hasSelected () {
      return this.selectedRowKeys.length > 0
    },
    handleAdd () {
      this.$refs.editForm.openForm(null, '新建字典信息')
    },
    handleEdit (id) {
      this.$refs.editForm.openForm(id, '编辑字典信息')
    },
    handleDelete (ids) {
      const thisObj = this
      this.$confirm({
        title: '确认删除吗?',
        onOk () {
          return new Promise((resolve, reject) => {
            thisObj.$http.post('/Base_BasicData/Base_DictionaryMain/DeleteData', ids).then(resJson => {
              resolve()
              if (resJson.Success) {
                thisObj.$message.success('操作成功!')
                thisObj.getDataList()
              } else {
                thisObj.$message.error(resJson.Msg)
                thisObj.getDataList()
              }
            })
          })
        }
      })
    }
  }
}
</script>
